package com.smart.community.feign.region.fallback;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.smart.community.feign.common.BaseFeignFallbackFactory;
import com.smart.community.feign.region.dto.OwnerInfoDTO;
import com.smart.community.feign.region.service.OwnerFeignService;

import lombok.extern.slf4j.Slf4j;

/**
 * 业主Feign降级处理工厂
 * 
 * 继承BaseFeignFallbackFactory，使用统一的降级处理模板
 * 当业主服务不可用时，提供柔和的降级处理，避免大量错误日志
 * 确保系统稳定性，不影响其他业务功能
 * 
 * @author Wu.Liang
 * @version 2.0.0
 * @since 2025-08-20
 * @updated 2025-01-30 - 继承BaseFeignFallbackFactory，使用统一的降级处理模板
 */
@Slf4j
@Component
public class OwnerFeignFallbackFactory extends BaseFeignFallbackFactory<OwnerFeignService> {

    @Override
    public OwnerFeignService create(Throwable cause) {
        // 使用基类的统一日志记录方法，区分异常类型
        if (isServiceUnavailable(cause)) {
            logServiceUnavailable(getServiceName(), cause);
        } else {
            log.warn("{}服务调用异常，执行降级处理: {}", getServiceName(), cause.getMessage());
        }
        
        return new OwnerFeignService() {
            
            @Override
            public OwnerInfoDTO getOwnerById(Long ownerId) {
                logMethodFallback("getOwnerById", ownerId);
                return new OwnerInfoDTO(); // 返回空对象，不影响业务
            }
            
            @Override
            public OwnerInfoDTO getOwnerByUserId(Long userId) {
                logMethodFallback("getOwnerByUserId", userId);
                return new OwnerInfoDTO(); // 返回空对象，不影响业务
            }
            
            @Override
            public OwnerInfoDTO getOwnerByIdCard(String idCard) {
                logMethodFallback("getOwnerByIdCard", idCard);
                return new OwnerInfoDTO(); // 返回空对象，不影响业务
            }
            
            @Override
            public OwnerInfoDTO getOwnerByPhone(String phone) {
                logMethodFallback("getOwnerByPhone", phone);
                return new OwnerInfoDTO(); // 返回空对象，不影响业务
            }
            
            @Override
            public List<OwnerInfoDTO> getOwnersByHouseholdId(Long householdId) {
                logMethodFallback("getOwnersByHouseholdId", householdId);
                return new ArrayList<>(); // 返回空列表，不影响业务
            }
            
            @Override
            public List<OwnerInfoDTO> getOwnersByCommunityId(Long communityId) {
                logMethodFallback("getOwnersByCommunityId", communityId);
                return new ArrayList<>(); // 返回空列表，不影响业务
            }
            
            @Override
            public List<OwnerInfoDTO> getOwnersByBuildingId(Long buildingId) {
                logMethodFallback("getOwnersByBuildingId", buildingId);
                return new ArrayList<>(); // 返回空列表，不影响业务
            }
            
            @Override
            public List<OwnerInfoDTO> getOwnersByUnitId(Long unitId) {
                logMethodFallback("getOwnersByUnitId", unitId);
                return new ArrayList<>(); // 返回空列表，不影响业务
            }
            
            @Override
            public Boolean checkOwnerExists(Long ownerId) {
                logMethodFallback("checkOwnerExists", ownerId);
                return false; // 返回false，不影响业务
            }
            
            @Override
            public Boolean checkIdCardExists(String idCard, Long excludeId) {
                logMethodFallback("checkIdCardExists", idCard, excludeId);
                return false; // 返回false，不影响业务
            }
            
            @Override
            public Boolean checkPhoneExists(String phone, Long excludeId) {
                logMethodFallback("checkPhoneExists", phone, excludeId);
                return false; // 返回false，不影响业务
            }
            
            @Override
            public List<Long> getOwnerHouseholdIds(Long userId) {
                logMethodFallback("getOwnerHouseholdIds", userId);
                return new ArrayList<>(); // 返回空列表，不影响业务
            }
            
            @Override
            public List<Long> getOwnerHouseholdIdsByOwnerId(Long ownerId) {
                logMethodFallback("getOwnerHouseholdIdsByOwnerId", ownerId);
                return new ArrayList<>(); // 返回空列表，不影响业务
            }
            
            @Override
            public List<Long> getOwnerHouseholdIdsByPropertyCompanies(Long userId, List<Long> propertyCompanyIds) {
                logMethodFallback("getOwnerHouseholdIdsByPropertyCompanies", userId, propertyCompanyIds);
                return new ArrayList<>(); // 返回空列表，不影响业务
            }
        };
    }
    
    @Override
    protected String getServiceName() {
        return "业主服务";
    }
}
